using System;

namespace FluentBinding.WinForms
{
    internal class EntityToPropertyConvertor<E, P> : IPropertyConvertor<E, P>
    {
        #region Fields

        private readonly Action<E, P> _initializer;
        private readonly Func<E, P> _selector;

        #endregion

        #region Constructors

        public EntityToPropertyConvertor(Func<E, P> selector)
            : this(selector, null)
        {
        }

        public EntityToPropertyConvertor(Func<E, P> selector, Action<E, P> initializer)
        {
            _selector = selector;
            _initializer = initializer;
        }

        #endregion

        #region IPropertyConvertor<E,P> Members

        public P Select(E entity)
        {
            return _selector(entity);
        }

        public void Initialize(E entity, P value)
        {
            if (_initializer != null)
                _initializer(entity, value);
        }

        #endregion
    }
}